package com.chen.solution.validator.demo.controller;

import com.chen.solution.validator.demo.api.BaseResponse;
import com.chen.solution.validator.demo.dto.CompanyDto;
import com.chen.solution.validator.demo.dto.GenericCompanyResponse;
import com.chen.solution.validator.demo.validation.Group1;
import com.chen.solution.validator.demo.validation.Group2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.security.krb5.internal.AuthContext;

import java.util.UUID;

/**
 * @author Chentian
 * @date 2022/3/16
 */
@Slf4j
@RestController
@RequestMapping("/v1/company")
@Validated
public class CompanyController {

    @PostMapping(path = "/create")
    public GenericCompanyResponse createCompany(@RequestBody @Validated({Group2.class}) CompanyDto companyDto) {
        log.info("name:{}", companyDto.getName());
        companyDto.setId(UUID.randomUUID().toString());
        return new GenericCompanyResponse(companyDto);
    }

    @GetMapping(path= "/get/{id}")
    public GenericCompanyResponse getCompany(@PathVariable String id) {
        log.info("id:{}", id);
        CompanyDto companyDto = CompanyDto.builder().id(id).name("test").build();
        return new GenericCompanyResponse(companyDto);
    }

    @PutMapping(path= "/update")
    public GenericCompanyResponse updateCompany(@RequestBody @Validated({Group1.class}) CompanyDto companyDto) {
        log.info("id:{},name:{}", companyDto.getId(), companyDto.getName());
        CompanyDto updatedCompanyDto = companyDto;
        return new GenericCompanyResponse(updatedCompanyDto);
    }
}
